<?php 

namespace app\zhonghang\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\Cache;
require_once (APP_PATH . 'api/SmsApi.php');
require_once (APP_PATH . 'api/FinanceApi.php');
class Partner extends Base {
    //空操作
    public function _initialize(){
    	parent::_initialize();
    }
    public function _empty(){
    	
        header("HTTP/1.0 404 Not Found");
        $this->display('Public:404');
    }
    /**
     * 获得个人信息
     */
    public function getRelation(){
    	$member = $this->member;
    	$re['pic'] = "/public/uploads/touxiang.png";
    	$re['phone'] = $member['phone'];
    	if($member['id_authentication']==0){
            $re['name']='未认证';
        }else{
            $re['name'] = $member['name'];
        }
    	$arr['status'] = 1;
    	$arr['member'] = $re;
    	$arr['info'] = "成功";
    	$data['data'] = $arr;
    	return json($data);
    }
    /**
     * 获得会员的账户信息
     * @return Ambigous <\think\response\Json, \think\Response, \think\response\View, \think\response\Xml, \think\response\Redirect, \think\response\Jsonp, unknown, \think\Response>
     */
    public function getMemberAccount(){
    	$member = $this->member;
    	$member_account = Db::name('App_member_account')->where(array('member_id'=>$member['member_id']))->find();
    	$arr['status'] = 1;	
    	$arr['account'] = $member_account['account_type_1'];
    	$arr['info'] = "成功";
    	$data['data'] = $arr;
    	return json($data);
    }
    /**
     * 获得会员银行卡信息
     */
    public function getBankList(){
    	$member = $this->member;
    	$where['uid'] = $member['member_id'];
    	$where['status'] = 1;
    	$list = Db::name('App_bank')->field('id,uid,bank_id,cardnum,cardname')->where($where)->select();
    	if(!empty($list)){
    		foreach($list as $k=>$v){
    			$bank = Db::name('App_bank_config')->where(array('id'=>$list[$k]['bank_id']))->find();
    			$list[$k]['bank_name'] = $bank['name'];
    			$list[$k]['logo'] = $bank['logo'];
    		}
    	}
    	if(empty($list)){
    		$arr['status'] = -1;
    		$arr['list'] = array();
    		$arr['info'] = "暂无记录";
    		$data['data'] = $arr;
    		return json($data);
    	}else{
    		$arr['status'] = 1;
    		$arr['list'] = $list;
    		$arr['info'] = "成功";
    		$data['data'] = $arr;
    		return json($data);
    	}
    }
    /**
     * 删除银行卡
     */
    public function deleteBank(){
    	$member = $this->member;
    	$id = input('id');
    	$bank = Db::name('App_bank')->where(array('id'=>$id))->find();
    	if($bank['uid'] != $member['member_id']){
    		$arr['status'] = -1;
    		$arr['info'] = "无权限";
    		$data['data'] = $arr;
    		return json($data);
    	}
    	$status['status'] = 0;
    	$res = Db::name('App_bank')->where(array('id'=>$id))->update($status);
    	if($res){
    		$arr['status'] = 1;
    		$arr['info'] = "成功";
    		$data['data'] = $arr;
    		return json($data);
    	}else{
    		$arr['status'] = -2;
    		$arr['info'] = "失败";
    		$data['data'] = $arr;
    		return json($data);
    	}
    }
    
    /**
     * 获得银行信息（配置信息）
     */
    public function getBankConfig(){
    	$list = Db::name('App_bank_config')->where(array('status'=>1))->select();
    	if(empty($list)){
    		$arr['status'] = -1;
    		$arr['list'] = array();
    		$arr['info'] = "暂无记录";
    		$data['data'] = $arr;
    		return json($data);
    	}else{
    		$arr['status'] = 1;
    		$arr['list'] = $list;
    		$arr['info'] = "成功";
    		$data['data'] = $arr;
    		return json($data);
    	}
    }
    /**
     * 添加银行卡（个人银行卡）
     */
    public function addBankCard(){
    	$member = $this->member;
    	$cardname = input('cardname');
    	$bank_id = input('bank_id');
    	$cardnum = input('cardnum');
    	if($cardname==null || $bank_id==null || $cardnum==null){
            $arr['status']=-4;
            $arr['info'] = '请填写内容';
            $data['data'] = $arr;
            return json($data);
        }
    	$res = $this->regex('bankcard', $cardnum);
    	if(!res){
    		$arr['status'] = -1;
    		$arr['info'] = "银行卡格式有误";
    		$data['data'] = $arr;
    		return json($data);
    	}
    	$bank['uid'] = $member['member_id'];
    	$bank['cardnum'] = $cardnum;
    	$bank['bank_id'] = $bank_id;
    	$bank['status'] = 1;
    	$bank['cardname'] = $cardname;
    	$re = Db::name('App_bank')->insert($bank);
    	if($re){
    		$arr['status'] = 1;
    		$arr['info'] = "成功";
    		$data['data'] = $arr;
    		return json($data);
    	}else{
    		$arr['status'] = -2;
    		$arr['info'] = "失败";
    		$data['data'] = $arr;
    		return json($data);
    	}
    }
    /**
     * 提现
     */
    public function cash(){
        //接银行卡id
    	$bank_id = input('bank_id');
    	$money = input("money");//获取转的金额 默认是0w
        //日志类型
    	$type = 1;
    	
    	$member = $this->member;
    	//防止重复提交
    	$trade = Cache::get('tradeMoney_'.$member['member_id']);
    	if($trade == true){
    		$msg['status'] = -2;
    		$msg['info'] = "请勿频繁操作";
    		return json($msg);exit;
    	}
    	//设置缓存
    	Cache::set('tradeMoney_'.$member['member_id'],true,5);
    	
   
    	$time_res = $this->panduanTime();
    	if(!$time_res){
    		$data['info'] = '当前时间无法交易';
    		$data['status'] = -11;
    		return json($data);
    	}
    	
    	if(empty($money)){
    		$msg['status']=-5;
    		$msg['info']="请输入转账金额";
    		return json($msg);exit;
    	}
//    	if(empty($bank_id)){
//            $msg['status']=-5;
//            $msg['info']="请输入转入的银行卡";
//            return json($msg);
//        }

    	//账户信息
    	//获取要转账的币种的余额
    	//获取所有的币种余额
    	$account = Db::name('App_member_account')->field('account_type_'.$type)->where(array('member_id'=>$member['member_id']))->find();
    	$account_type_one = M('App_account_type')->where(array('id'=>$type))->find();//获取币种的信息
    	if($account['account_type_'.$type]<$money){
    		$msg['status']=-6;
    		$msg['info']= $account_type_one['name']."不足";
    		return json($msg);exit;
    	}
    	Db::startTrans();
    	//添加记录
    	$config = $this->configs();
    	//用户id
    	$data['member_id'] = $member['member_id'];
    	//钱数
    	$data['num'] = $money;
        $bank = Db::name('App_bank')->where(array('id'=>$bank_id))->find();
    	//银行卡号
    	$data['bank_id'] = $bank['cardnum'];
    	//提现时间
    	$data['add_time'] = time();
    	//提现手续费
    	$data['withdraw_fee'] = $money * $config['tibi_fee'] *0.01;
    	$data['status'] = 0;
    	//插入数据
    	$res[] = Db::name('Withdraw')->insert($data);
    	//对应减钱
    	$re[]=db("App_member_account")->where(['member_id'=>$member['member_id']])->setDec('account_type_'.$type,$money);
    	$Finance = new \FinanceApi();
    	$re[]=$Finance->addFinance($member['member_id'], 1, "提现".$money."元,到". $bank['bankname'].'尾号'.substr($bank['cardnum'],-4),'2',$money-$data['withdraw_fee'],$type);
    	if(!empty($data['withdraw_fee'])){
    		$re[]=$Finance->addFinance($member['member_id'], 8, "提现".$money."手续费：".$data['withdraw_fee'],'2',$data['withdraw_fee'],$type);
    	}
    	//     	dump($re);
    	if($re){
    		Db::commit();
    		$msg['status']=1;
    		$msg['info']="成功";
    		return json($msg);
    	}else{
    		Db::rollback();
    		$msg['status']=-7;
    		$msg['info']="失败";
    		return json($msg);
    	}
    }
    /**
     * 判断提现时间
     * @return boolean
     */
    public function panduanTime(){
    	$checkDayStr = date('Y-m-d ',time());
    	$config = $this->configs();
    	$timeBegin1 = strtotime($checkDayStr.$config['tixian_begin_time'].":00");
    	$timeEnd1 = strtotime($checkDayStr.$config['tixian_end_time'].":00");
    	$curr_time = time();
    	if($curr_time >= $timeBegin1 && $curr_time <= $timeEnd1)
    	{
    		return true;
    	} 
    	return false;
    }
    /**
     * 获得提现记录
     */
    public function getWithdrawList(){
    	$member = $this->member;
    	$where['finance_type'] = 1;
    	$where['member_id'] = $member['member_id'];
    	$list = Db::name('App_finance')->where($where)->paginate(20,false,['query'=>input()]);
    	//按月分组
    	foreach($list as $key => $val){
    		$date = getdate($val['add_time']);
    		for($i = 1; $i<12;$i++){
    			if($date['mon'] == $i){
    				$show[$i][] = $val;
    			}
    		}
    	}
		$page = $list->render ();
		$list = $list->all();
    	if($list){
			$arr['status'] = 1;
			$arr['info'] = "成功";
			$arr['list'] = $show;
			$data['data'] = $arr;
			return json($data);
		}else{
			$arr['status'] = -2;
			$arr['info'] = "暂无记录";
			$arr['list'] = $list;
			$data['data'] = $arr;
			return json($data);
		}
    }
    /**
     * 收益记录
     */
    public function getIncomeList(){
    	$member = $this->member;
    	//当月收益
    	$start = strtotime(date('Y-m-d',time()));
    	$end = $start+84600;
    	$time[] = $start;
    	$time[] = $end;
    	$map_today['add_time'] = array('between',$time);
    	$map_today['member_id'] = $member['member_id'];
    	$map_today['finance_type'] = 3;
    	$today_money = Db::name('App_finance')->where($map_today)->sum('money');

    	if(empty($today_money)){
            $today_money=0;
        }
    	//历史收益
    	$map_all['member_id'] = $member['member_id'];
    	$map_all['finance_type'] = 3;
    	$all_money = Db::name('App_finance')->where($map_all)->sum('money');
    	
    	
    	$where['finance_type'] = 3;
    	$where['member_id'] = $member['member_id'];
    	$list = Db::name('App_finance')->where($where)->paginate(20,false,['query'=>input()]);


        //按月分组
    	foreach($list as $key => $val){
    		$date = getdate($val['add_time']);
    		for($i = 1; $i<12;$i++){
    			if($date['mon'] == $i){
    				$show[$i][] = $val;
    			}
    		}
    	}
    	$page = $list->render ();
    	$list = $list->all();
    	if($list){
    		$arr['status'] = 1;
    		$arr['info'] = "成功";
    		$arr['list'] = $show;
    		$arr['today_money'] = $today_money;
    		$arr['all_money'] = $all_money;
    		$data['data'] = $arr;
    		return json($data);
    	}else{
    		$arr['status'] = -2;
    		$arr['info'] = "暂无记录";
    		$arr['list'] = $show;
    		$arr['today_money'] = $today_money;
    		$arr['all_money'] = $all_money;
    		$data['data'] = $arr;
    		return json($data);
    	}
    	
    }
    
    
    
    
    
    /**
     * 获得邀请名单
     */
    public function getTeam(){
    	$member = $this->member;
    	$list = Db::name('App_member_relation')->field('level,name,phone,member_id,zhuceid,add_time')->where(array('zhuceid'=>$member['member_id']))->select();
    	foreach($list as $k=>$v){
    		$level = Db::name('App_level')->where(array('id'=>$list[$k]['level']))->find();
    		$list[$k]['level_name'] = $level['name'];
    	}
    	if($list){
			$arr['status'] = 1;
			$arr['info'] = "成功";
			$arr['list'] = $list;
			$data['data'] = $arr;
			return json($data);
		}else{
			$arr['status'] = -2;
			$arr['info'] = "暂无记录";
			$arr['list'] = $list;
			$data['data'] = $arr;
			return json($data);
		}
    }
    /**
     * 获得我得消息
     */
    public function getMessage(){
    	$member = $this->member;
    	$up['status']='1';

    	$message = Db::name('Message')->where(array('to_member_id'=>$member['member_id']))->order('add_time desc')->paginate(20,false,['query'=>input()]);
		$page = $message->render ();
		$message = $message->all();
        DB::name('Message')->where('to_member_id',$member['member_id'])->update($up);
		if($message){
			$arr['status'] = 1;
			$arr['info'] = "成功";
			$arr['message'] = $message;
			$data['data'] = $arr;
			return json($data);
		}else{
			$arr['status'] = -2;
			$arr['info'] = "暂无记录";
			$arr['message'] = $message;
			$data['data'] = $arr;
			return json($data);
		}
    }
    
    
    
    
    
    
    
    
    
 	/**
 	 * 获得商户的邀请码
 	 */
    public function getMerchantQRcode(){
    	$member = $this->member;
    	$code = $member['code'];
    	$level = 1;
    	$md = md5($code.$level);
    	$list['url'] = 'http://flcm.qianyiwangluo.com/home/Reg/index?code='.$code.'_'.$md;
    	$list['pic'] = '/public/uploads/touxiang.png';
    	$list['code'] = $member['code'];
    	$list['name'] = $member['name'];
    	$arr['status'] = 1;
		$arr['list'] = $list;
		$arr['info'] = "成功";
		$data['data'] = $arr;
		return json($data);
    }
    /**
     * 获得合伙人的邀请码
     */
    public function getPartnerQRcode(){
    	$member = $this->member;
    	$code = $member['code'];
    	$level = 2;
    	$md = md5($code.$level);
    	$list['url'] = 'http://flcm.qianyiwangluo.com/home/Reg/index?code='.$code.'_'.$md;
    	$list['pic'] = '/public/uploads/touxiang.png';
    	$list['code'] = $member['code'];
    	$list['name'] = $member['name'];
    	$arr['status'] = 1;
    	$arr['list'] = $list;
    	$arr['info'] = "成功";
    	$data['data'] = $arr;
    	return json($data);
    }
    
    
    
}